package org.he.auth.service.impl;

import org.he.auth.domain.Role;
import org.he.auth.dto.RoleMenuDTO;
import org.he.auth.dto.RolePermissionDTO;
import org.he.auth.mapper.RoleMapper;
import org.he.auth.service.IRoleService;
import org.he.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author he
 * @since 2023-12-02
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void saveAuth(RolePermissionDTO dto) {
        // 方案一 删除这一次 dto中对应的id 不存在的list
        // 方案二 将dto中id的相关数据全删了 再重写
        roleMapper.deleteAuth(dto.getId());
        if (dto.getPermissions().size() != 0) {
            roleMapper.saveAuth(dto);
        }
    }
    @Override
    public void saveMenu(RoleMenuDTO dto) {
        roleMapper.deleteMenu(dto.getId());
        if (dto.getMenus().size() != 0){
            roleMapper.saveMenu(dto);
        }
    }

    @Override
    public List<Long> getPermissionById(Long id) {
        return roleMapper.getPermissionById(id);
    }

    @Override
    public List<Long> getMenusById(Long id) {
        return roleMapper.getMenusById(id);
    }


    @Override
    public void delete(Serializable id) {
        roleMapper.deleteAuth((Long) id);
        super.delete(id);
    }

    @Override
    public void batchDelete(List<Long> ids) {
        for (Long id : ids) {
            roleMapper.deleteAuth(id);
        }
        super.batchDelete(ids);
    }
}
